
package com.langcollab.languagementor.component
{
    // We're using composition instead of subclassing TreeList because we may
    // want the ability to specify the tree list class in the future. For
    // example, we may develop a CheckboxTreeList. But it might be better
    // to subclass TreeList and pass in a custom ItemRenderer.

    import com.brightworks.component.list.Event_TreeList;
    import com.brightworks.component.list.TreeItem;
    import com.brightworks.component.list.TreeList;
    import com.langcollab.languagementor.component.event.Event_List_ModuleSelector;
    import com.langcollab.languagementor.event.Event_ApplicationState;
    import com.langcollab.languagementor.vo.ModuleVersionVO;

    import mx.collections.ArrayCollection;

    import spark.components.Group;
    import spark.events.IndexChangeEvent;

    public class List_ModuleSelector extends Group
    {
        public var allowMultipleSelection:Boolean;

        private var _currentParentTreeItem:TreeItem;
        private var _selectedModuleVersionVOs:ArrayCollection;
        private var _treeData:ArrayCollection;
        private var _treeList:TreeList;

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Public Methods
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        public function List_ModuleSelector(treeData:ArrayCollection, selectedModules:ArrayCollection)
        {
            super();
            _treeData = treeData;
            _selectedModuleVersionVOs = new ArrayCollection();
            _selectedModuleVersionVOs.addAll(selectedModules);
            createTreeListComponent(_treeData);
        }

        public function dispose():void
        {
            if (_treeList)
            {
                removeElement(_treeList);
                _treeList = null;
            }
        }

        public function handleBackButton():void
        {
            _currentParentTreeItem = _currentParentTreeItem.parent;
            if (_currentParentTreeItem)
            {
                createTreeListComponent(_currentParentTreeItem.children);
            }
            else
            {
                createTreeListComponent(_treeData);
            }
        }

        public function isListAtTopLevel():Boolean
        {
            return (!_currentParentTreeItem);
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Private Methods
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        private function createSelectedIndicesVectorForLeafList(dataProvider:ArrayCollection):Vector.<int>
        {
            var result:Vector.<int>;
            for (var i:int = 0; i < dataProvider.length; i++)
            {
                var ti:TreeItem = TreeItem(dataProvider[i]);
                var vo:ModuleVersionVO = ti.data.moduleVersionVO;
                if (_selectedModuleVersionVOs.getItemIndex(vo) != -1)
                {
                    result.push(i)
                }
            }
            return result;
        }

        private function createTreeListComponent(dataProvider:ArrayCollection, isLeafs:Boolean = false):void
        {
            if (_treeList)
            {
                _treeList.dataProvider = null;
                removeElement(_treeList);
            }
            _treeList = new TreeList();
            _treeList.percentHeight = 100;
            _treeList.percentWidth = 100;
            _treeList.dataProvider = dataProvider;
            if (isLeafs)
            {
                _treeList.allowMultipleSelection = allowMultipleSelection;
                _treeList.selectedIndices = createSelectedIndicesVectorForLeafList(dataProvider);
                _treeList.addEventListener(Event_TreeList.TOGGLE_LEAF_ITEM, onLeafsTreeListChange);
            }
            else
            {
                _treeList.allowMultipleSelection = false;
                _treeList.addEventListener(IndexChangeEvent.CHANGE, onBranchesTreeListChange);
            }
            addElement(_treeList);
        }

        private function onBranchesTreeListChange(event:IndexChangeEvent):void
        {
            if (!_treeList)
                return;
            _currentParentTreeItem = _treeList.selectedItem;
            createTreeListComponent(_currentParentTreeItem.children, _currentParentTreeItem.areAllChildrenLeafs());
        }

        private function onLeafsTreeListChange(event:Event_TreeList):void
        {
            if (!_treeList)
                return;
            var vo:ModuleVersionVO = event.leafData.data.moduleVersionVO; 
            var voIndex:int = _selectedModuleVersionVOs.getItemIndex(vo);
            if (voIndex == -1)
            {
                _selectedModuleVersionVOs.addItem(vo);
            }
            else
            {
                _selectedModuleVersionVOs.removeItemAt(voIndex);
            }
            var e:Event_List_ModuleSelector = new Event_List_ModuleSelector(Event_List_ModuleSelector.TOGGLE_MODULE_SELECTED);
            e.moduleVersionVO = vo;
            dispatchEvent(e);
        }

    }
}

